import {
  BelongsToMany,
  Column,
  HasOne,
  Model,
  Table,
} from 'sequelize-typescript';
import { UserExtend } from './user-extend.model';
import { Role } from 'src/role/role.model';

@Table({
  // 模型名
  modelName: 'User',
  // 表名
  tableName: 'users',
  // 开启软删除
  paranoid: true,
  // 指定软删除字段
  deletedAt: 'deleted_at',
  // 命名策略：表字段使用 snake_case，模型属性使用 camelCase
  underscored: true,
  // 自定义 createdAt 和 updatedAt 字段的格式
  timestamps: true,
  createdAt: 'created_at', // 将 created_at 字段重命名为 custom_created_at
  updatedAt: 'updated_at', // 将 updated_at 字段重命名为 custom_updated_at
})
export class User extends Model {
  @Column
  type: number;

  @Column
  status: number;

  @Column
  sex: number;

  @Column
  username: string;

  @Column
  password: string;

  @Column
  mobile: string;

  @Column
  email: string;

  @Column
  avatar: string;

  @Column
  nickname: string;

  @Column
  real_name: string;

  @Column
  id_card: string;

  @Column
  description: string;

  @Column
  label: string;

  // @HasOne(() => UserExtend, { foreignKey: 'user_id' })
  // or
  @HasOne(() => UserExtend, 'user_id')
  extend: UserExtend;

  @BelongsToMany(() => Role, 'user_roles', 'user_id', 'role_id')
  roles: Role[];
}
